package com.edu.hre.struts.action.survey;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.RequestUtils;
import com.edu.hre.db.pojo.HreSurveyEmpAnswer;
import com.edu.hre.db.pojo.HreSurveyQuestion;
import com.edu.hre.db.pojo.HreSurveyQuestionAnswer;
import com.edu.hre.db.pojo.HreSurveyTopic;
import com.edu.hre.db.pojo.HreUser;
import com.edu.hre.db.pojo.HreUserRoleScreen;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.survey.SurveyFreeTextListForm;
import com.edu.hre.struts.form.survey.SurveySummaryDetailForm;
import com.google.gwt.http.client.Response;

public class SurveyFreeTextListAction extends DispatchAction {
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	
	public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		HreUserRoleScreen userRoleScreen = (HreUserRoleScreen) request.getSession().getAttribute(RequestUtils.Attribute.HreUserRoleScreen);
		HreUser user = (HreUser) request.getSession().getAttribute(RequestUtils.Attribute.USER_INFO);
		SurveyFreeTextListForm beanForm = (SurveyFreeTextListForm) form;
		int id = Integer.parseInt(request.getParameter("id"));
		HreSurveyQuestionAnswer answer = baseService.getPojo(id, HreSurveyQuestionAnswer.class);
		HreSurveyQuestion question = baseService.getPojo(answer.getQuestionId(), HreSurveyQuestion.class);
		HreSurveyTopic topic = baseService.getPojo(question.getTopicId(), HreSurveyTopic.class);
		if(topic.getShowToAll() == false && topic.getUserCreated() != user.getId()){
			response.sendError(Response.SC_BAD_REQUEST);
			return null;
		}
		List<HreSurveyEmpAnswer> listEmpAnswer = baseService.getPojos("answer", Compare.EQUAL, answer.getId(), HreSurveyEmpAnswer.class);
		beanForm.setListEmpAnswer(listEmpAnswer);
		beanForm.setAnswer(answer.getText());
		beanForm.setQuestion(question.getText());
		beanForm.setTopic(topic.getName());
		
		return mapping.findForward("surveyFreeTextList");
	}
}
